package cluster

import (
	"go-redis/go-redis/interface/resp"
	"go-redis/go-redis/resp/reply"
)

func Rename(clusterDatabase *ClusterDatabase, connection resp.Connection, cmdArgs [][]byte) resp.Reply {

	if len(cmdArgs) != 3 {
		return reply.MakeErrReply("ERR wrong number of arguments for 'rename' command")
	}
	src := string(cmdArgs[1])
	dest := string(cmdArgs[2])

	srcPeer := clusterDatabase.peerPicker.PickNode(src)
	destPeer := clusterDatabase.peerPicker.PickNode(dest)

	if srcPeer != destPeer {
		return reply.MakeErrReply("ERR rename must within one slot in cluster mode")
	}
	return clusterDatabase.relay(srcPeer, connection, cmdArgs)
}
